const Controller = require("egg").Controller;

class UserController extends Controller {
  // 获取用户列表
  async getUserList() {
    const { ctx } = this;
    const { userName, nickName } = ctx.query;
    try {
      let userList = [];
      if (userName || nickName) {
        //模糊查询用户
        userList = await this.ctx.service.user.getLikeUserList(
          userName,
          nickName
        );
      } else {
        userList = await this.ctx.service.user.getUserList();
      }
      this.ctx.body = {
        code: 200,
        data: userList,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 保存用户
  async saveUser() {
    const { ctx } = this;
    const { id, userName, nickName, passWord } = ctx.request.body;
    try {
      const result = await this.ctx.service.user.saveUser({
        id,
        userName,
        nickName,
        passWord,
      });
      this.ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  //修改用户
  async modifyUser() {
    const { ctx } = this;
    const { userName, nickName, headPicture } = ctx.request.body;
    const { id } = ctx.decode;
    try {
      const result = await this.ctx.service.user.modifyUser({
        id,
        userName,
        nickName,
        headPicture,
      });
      this.ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 获取用户
  async findUser() {
    const { ctx } = this;
    // 获取token解析后的用户信息
    const { id } = ctx.decode;
    try {
      const user = await this.ctx.service.user.findUserById(id);
      this.ctx.body = {
        code: 200,
        data: user,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 删除用户
  async deleteUser() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    try {
      const user = await this.ctx.service.user.deleteUser(id);

      this.ctx.body = {
        code: 200,
        data: user,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 用户角色
  async findUserSelfRoles() {
    const { ctx } = this;
    const { userId } = ctx.query;
    try {
      const userRoles = await this.ctx.service.user.findUserSelfRoles(userId);
      ctx.body = {
        code: 200,
        data: userRoles.map((userRole) => {
          return userRole.roleId;
        }),
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 用户授权
  async userAddRole() {
    const { ctx } = this;
    const { userId, roleIds } = ctx.request.body;

    try {
      const userRole = await ctx.service.user.userAddRole(userId, roleIds);
      this.ctx.body = {
        code: 200,
        data: userRole,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }
}

module.exports = UserController;
